package com.villaday.idf.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.villaday.idf.AppContext;
import com.villaday.idf.base.greendao.ChaozhiTemaiHistoryDao;
import com.villaday.idf.base.greendao.DaoMaster;
import com.villaday.idf.base.greendao.DaoSession;
import com.villaday.idf.base.greendao.HDao;
import com.villaday.idf.model2.ChaozhiTemaiHistory;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by tudou on 2018/10/17.
 */

public class DbHomeTeMaiHistoryUtils {

    private static DbHomeTeMaiHistoryUtils db;
    private final static String dbName = "hometemaihistory_db";
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;


    public DbHomeTeMaiHistoryUtils() {
        context = AppContext.getInstance();
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
    }


    /**
     * 获取单例
     *
     * @return
     */
    public static DbHomeTeMaiHistoryUtils getInstance() {
        if (db == null) {
            synchronized (DbHomeTeMaiHistoryUtils.class) {
                if (db == null) {
                    db = new DbHomeTeMaiHistoryUtils();
                }
            }
        }
        return db;
    }


    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }


    public void save(ChaozhiTemaiHistory info) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        ChaozhiTemaiHistoryDao downInfoDao = daoSession.getChaozhiTemaiHistoryDao();
        downInfoDao.insert(info);
    }


    public void deleteDowninfo(ChaozhiTemaiHistory info) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        ChaozhiTemaiHistoryDao downInfoDao = daoSession.getChaozhiTemaiHistoryDao();
        downInfoDao.delete(info);
    }

    public void deleteAll() {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        ChaozhiTemaiHistoryDao downInfoDao = daoSession.getChaozhiTemaiHistoryDao();
        downInfoDao.deleteAll();
    }


    public List<ChaozhiTemaiHistory> queryDownAll() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        ChaozhiTemaiHistoryDao downInfoDao = daoSession.getChaozhiTemaiHistoryDao();
        QueryBuilder<ChaozhiTemaiHistory> qb = downInfoDao.queryBuilder();
        return qb.list();
    }

    public List<ChaozhiTemaiHistory> queryDownAllOrderByDate() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        ChaozhiTemaiHistoryDao downInfoDao = daoSession.getChaozhiTemaiHistoryDao();
        QueryBuilder<ChaozhiTemaiHistory> qb = downInfoDao.queryBuilder()
                .orderDesc(ChaozhiTemaiHistoryDao.Properties.CurrentPrice);
        return qb.list();
    }

    public List<ChaozhiTemaiHistory> queryWithSomethingAll(String something) {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        ChaozhiTemaiHistoryDao downInfoDao = daoSession.getChaozhiTemaiHistoryDao();
        List<ChaozhiTemaiHistory> items = downInfoDao.queryBuilder()
                .where(HDao.Properties.Name.like("%"+something + "%"))
                .list();
        return items;
    }
    public List<ChaozhiTemaiHistory> queryEqAll(String something) {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        ChaozhiTemaiHistoryDao downInfoDao = daoSession.getChaozhiTemaiHistoryDao();
        List<ChaozhiTemaiHistory> list = downInfoDao.queryBuilder()
                .where(ChaozhiTemaiHistoryDao.Properties.Keyword.eq(something)).list();


        return list;
    }

    public void insertOrReplace(String something,long date) {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        ChaozhiTemaiHistoryDao downInfoDao = daoSession.getChaozhiTemaiHistoryDao();
        downInfoDao.insertOrReplace(new ChaozhiTemaiHistory(something,date));


    }
}
